import { defineConfig, loadEnv } from 'vite'
import react from '@vitejs/plugin-react'
import path from 'path'

// https://vite.dev/config/
export default defineConfig(({ mode }) => {
  // 加载环境变量
  const env = loadEnv(mode, process.cwd(), '')
  const apiBaseUrl = env.VITE_API_BASE_URL || '/api';

  // 合并部署模式：输出到 Spring Boot 静态资源目录
  const isMergedMode = mode === 'production';
  const outDir = isMergedMode
    ? path.resolve(__dirname, '../api/src/main/resources/static')
    : 'dist';

  return {
    // 设置基础路径
    base: env.VITE_BASE_PATH || '/',

    plugins: [react()],

    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src'),
      },
    },

    build: {
      // 输出目录
      outDir: outDir,
      // 静态资源目录
      assetsDir: 'assets',
      // 启用/禁用 CSS 代码拆分
      cssCodeSplit: true,
      // 构建后是否生成 source map 文件
      sourcemap: false,
      // chunk 大小警告的限制（单位：KB）
      chunkSizeWarningLimit: 2000,
      rollupOptions: {
        output: {
          // 静态资源分类打包
          chunkFileNames: 'js/[name]-[hash].js',
          entryFileNames: 'js/[name]-[hash].js',
          assetFileNames: '[ext]/[name]-[hash].[ext]',
        },
      },
    },

    server: {
      port: 3000,
      proxy: {
        [apiBaseUrl]: {
          target: 'http://localhost:1249',
          changeOrigin: true,
          rewrite: (path) => path.replace(new RegExp(`^${apiBaseUrl}`), ''),
        },
      },
      historyApiFallback: true,
    },

    preview: {
      port: 3000,
      historyApiFallback: true,
    },
  }
})
